<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>
    
    位运算 - DSRBLOG
    
  </title>
  

  
  
  <link href="atom.xml" rel="alternate" title="DSRBLOG" type="application/atom+xml">
  <link rel="stylesheet" href="asset/css/style.min.css">
  <link rel="stylesheet" href="asset/css/doc.css">
  <script src="asset/app.js"></script>


</head>

<body> <section class="ct-body">
	<div class="container">
		<div class="columns is-variable bd-klmn-columns is-4 is-centered">
			<div class="column is-four-fifths">
				<div class="post-body single-content">
					
					<h1 class="title">
						位运算
					</h1>


					<div class="media">
						
						
						<div class="media-content">
							<div class="content">
								<p>
									<span class="date">2022/01/28 01:52 上午</span>
									

									

									<br />
									<span class="tran-tags">Tags:</span>&nbsp;
									
									<a class="tag is-link is-light">#C语言</a>
									

								</p>
							</div>
						</div>

					</div>
				</div>
				<article class="markdown-body single-content">
					<p>C语言提供了位运算的功能，这使得C语言也能像汇编语言一样用来编写系统程序</p>
<h2><a id="c%E8%AF%AD%E8%A8%80%E6%8F%90%E4%BE%9B%E7%9A%84%E5%85%AD%E7%A7%8D%E4%BD%8D%E8%BF%90%E7%AE%97%E7%AC%A6" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>C语言提供的六种位运算符</h2>
<pre class="line-numbers"><code class="language-c">&amp;   //按位与
|   //按位或
^   //按位异或
~   //按位取反
&lt;&lt;  //左移
&gt;&gt;  //右移
</code></pre>
<h3><a id="%E6%8C%89%E4%BD%8D%E4%B8%8E%EF%BC%88%EF%BC%89%EF%BC%9A" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>按位与（&amp;）：</h3>
<h4><a id="9-5-1" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>9 &amp; 5 = 1</h4>
<pre class="line-numbers"><code class="language-c"> 00001001 //9的二进制补码
&amp;00000101 //5的二进制补码

 00000001 //1的二进制补码
</code></pre>
<ul>
<li>对应位都为1，结果为1，否则为0</li>
</ul>
<h3><a id="%E6%8C%89%E4%BD%8D%E6%88%96%EF%BC%88%EF%BC%89" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>按位或（|）</h3>
<h4><a id="9-5-13" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>9 | 5 = 13</h4>
<pre class="line-numbers"><code class="language-c"> 00001001 //9的二进制补码
|00000101 //5的二进制补码

 00001101 //13的二进制补码
</code></pre>
<ul>
<li>对应位都为0，结果为0，否则为1</li>
</ul>
<h3><a id="%E6%8C%89%E4%BD%8D%E5%BC%82%E6%88%96%EF%BC%88%EF%BC%89" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>按位异或（^）</h3>
<h4><a id="9-5-12" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>9 ^ 5 = 12</h4>
<pre class="line-numbers"><code class="language-c"> 00001001 //9的二进制补码
^00000101 //5的二进制补码

 00001100 //12的二进制补码
</code></pre>
<ul>
<li>对应位相同，结果为0，否则为1</li>
</ul>
<h3><a id="%E6%8C%89%E4%BD%8D%E5%8F%96%E5%8F%8D%EF%BC%88%EF%BC%89%EF%BC%9A" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>按位取反（~）：</h3>
<h4><a id="9-10" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>!9 = -10</h4>
<pre class="line-numbers"><code class="language-c">~(0000000000001001) : 1111111111110110
</code></pre>
<h3><a id="%E5%B7%A6%E7%A7%BB%EF%BC%88%EF%BC%89%E5%92%8C%E5%8F%B3%E7%A7%BB%EF%BC%88%EF%BC%89" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>左移（&lt;&lt;）和右移（&gt;&gt;）</h3>
<h4><a id="a-3" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>a = 3</h4>
<pre class="line-numbers"><code class="language-c">a &lt;&lt; 2 = 12 //a左移两位
</code></pre>
<ul>
<li>左移一位相当于原值乘以2</li>
</ul>
<pre class="line-numbers"><code class="language-c">a &gt;&gt; 2 = 3 //a右移两位
</code></pre>
<ul>
<li>右移一位理论上相当于原值除以2，并舍弃小数点后位数，实际是二进制数向右移动后重新计算</li>
</ul>

				</article>
				<div class="comments-wrap">
					<div class="share-comments">
						

						

						
					</div>
				</div><!-- end comments wrap -->
			</div>
		</div><!-- end columns -->
	</div><!-- end container -->
</section>  














<script src="asset/prism.js"></script>









</body>

</html>